import ShortId from "shortid";
/*-工具类-*/
export default class Tool {
  // 过滤空值
  static filterParams(params) {
    if (params.length) {
      return;
    } else {
      const p = {};
      for (const key in params) {
        if (params.hasOwnProperty(key)) {
          const el = params[key];
          if (el !== "") {
            p[key] = el;
          }
        }
      }
      return p;
    }
  }

  static arrayToTree(data, pid, pKey = "pid", key = "id") {
    var tree = [];
    var temp;
    for (var i = 0; i < data.length; i++) {
      if (String(data[i][pKey]) === String(pid)) {
        const obj = data[i];
        obj.key = data[i][key];
        temp = this.arrayToTree(data, data[i][key], pKey, key);
        if (temp.length > 0) {
          obj.father = true;
          obj.children = temp;
        }
        tree.push(obj);
      }
    }
    return tree;
  }

  static uuid() {
    return ShortId.generate().substring(0, 6);
  }

  static generateShortUUID() {
    let d = new Date().getTime(); // 当前时间的毫秒数
    let d2 = (performance && performance.now && performance.now() * 1000) || 0; // 微秒级时间戳
    const str = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
      /[xy]/g,
      function (c) {
        let r = Math.random() * 16; // 生成0-16之间的随机数
        if (d > 0) {
          // 使用日期时间戳
          r = (d + r) % 16 | 0;
          d = Math.floor(d / 16);
        } else {
          // 如果没有Date.now支持，使用性能时间戳
          r = (d2 + r) % 16 | 0;
          d2 = Math.floor(d2 / 16);
        }
        return (c === "x" ? r : (r & 0x3) | 0x8).toString(16); // 生成0-F之间的16进制数
      }
    );

    return str.substring(0, 6);
  }

  // json对象排序
  static compare(arr, key) {
    return arr.sort(function (a, b) {
      var value1 = a[key];
      var value2 = b[key];
      return value1 - value2;
    });
  }
  static download(content, fileName) {
    const blob = new Blob([content]);
    const a = document.createElement("a");
    const url = window.URL.createObjectURL(blob);
    const filename = fileName + ".xlsx";
    a.href = url;
    a.download = filename;
    a.click();
    window.URL.revokeObjectURL(url);
  }

  static deepClone(obj) {
    if (obj === null || typeof obj !== "object") {
      return obj;
    }

    if (obj instanceof Date) {
      return new Date(obj.getTime());
    }

    if (obj instanceof Array) {
      return obj.reduce((arr, item, i) => {
        arr[i] = this.deepClone(item);
        return arr;
      }, []);
    }

    if (obj instanceof Object) {
      return Object.keys(obj).reduce((newObj, key) => {
        newObj[key] = this.deepClone(obj[key]);
        return newObj;
      }, {});
    }
  }

  static toObject(val) {
    let d = {};
    d[val.value] = {};
    const dep = (a) => {
      let c = {
        ...a,
      };
      if (a.children && a.children.length) {
        const children = a.children.map((item) => {
          return dep(item);
        });
        c.children = children;
      } else {
        d[val.value][a.value] = a.name;
      }
      return c;
    };
    const c = dep(val);
    return {
      c,
      d,
    };
  }
}
